/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#[derive(Debug, PartialEq, Clone, Copy, Hash)]
pub enum RequestCode {
    SendMessage = 10,
    PullMessage = 11,
    QueryMessage = 12,
    QueryBrokerOffset = 13,
    QueryConsumerOffset = 14,
    UpdateConsumerOffset = 15,
    UpdateAndCreateTopic = 17,
    GetAllTopicConfig = 21,
    GetTopicConfigList = 22,
    GetTopicNameList = 23,
    UpdateBrokerConfig = 25,
    GetBrokerConfig = 26,
    TriggerDeleteFiles = 27,
    GetBrokerRuntimeInfo = 28,
    SearchOffsetByTimestamp = 29,
    GetMaxOffset = 30,
    GetMinOffset = 31,
    GetEarliestMsgStoreTime = 32,
    ViewMessageById = 33,
    HeartBeat = 34,
    UnregisterClient = 35,
    ConsumerSendMsgBack = 36,
    EndTransaction = 37,
    GetConsumerListByGroup = 38,
    CheckTransactionState = 39,
    NotifyConsumerIdsChanged = 40,
    LockBatchMq = 41,
    UnlockBatchMq = 42,
    GetAllConsumerOffset = 43,
    GetAllDelayOffset = 45,
    CheckClientConfig = 46,
    GetClientConfig = 47,
    UpdateAndCreateAclConfig = 50,
    DeleteAclConfig = 51,
    GetBrokerClusterAclInfo = 52,
    UpdateGlobalWhiteAddrsConfig = 53,
    GetBrokerClusterAclConfig = 54, // Deprecated
    GetTimerCheckPoint = 60,
    GetTimerMetrics = 61,
    PopMessage = 200050,
    AckMessage = 200051,
    BatchAckMessage = 200151,
    PeekMessage = 200052,
    ChangeMessageInvisibleTime = 200053,
    Notification = 200054,
    PollingInfo = 200055,
    PutKvConfig = 100,
    GetKvConfig = 101,
    DeleteKvConfig = 102,
    RegisterBroker = 103,
    UnregisterBroker = 104,
    GetRouteinfoByTopic = 105,
    GetBrokerClusterInfo = 106,
    UpdateAndCreateSubscriptionGroup = 200,
    GetAllSubscriptionGroupConfig = 201,
    GetTopicStatsInfo = 202,
    GetConsumerConnectionList = 203,
    GetProducerConnectionList = 204,
    WipeWritePermOfBroker = 205,
    GetAllTopicListFromNameserver = 206,
    DeleteSubscriptionGroup = 207,
    GetConsumeStats = 208,
    SuspendConsumer = 209,
    ResumeConsumer = 210,
    ResetConsumerOffsetInConsumer = 211,
    ResetConsumerOffsetInBroker = 212,
    AdjustConsumerThreadPool = 213,
    WhoConsumeTheMessage = 214,
    DeleteTopicInBroker = 215,
    DeleteTopicInNamesrv = 216,
    RegisterTopicInNamesrv = 217,
    GetKvlistByNamespace = 219,
    ResetConsumerClientOffset = 220,
    GetConsumerStatusFromClient = 221,
    InvokeBrokerToResetOffset = 222,
    InvokeBrokerToGetConsumerStatus = 223,
    QueryTopicConsumeByWho = 300,
    GetTopicsByCluster = 224,
    QueryTopicsByConsumer = 343,
    QuerySubscriptionByConsumer = 345,
    RegisterFilterServer = 301,
    RegisterMessageFilterClass = 302,
    QueryConsumeTimeSpan = 303,
    GetSystemTopicListFromNs = 304,
    GetSystemTopicListFromBroker = 305,
    CleanExpiredConsumequeue = 306,
    GetConsumerRunningInfo = 307,
    QueryCorrectionOffset = 308,
    ConsumeMessageDirectly = 309,
    SendMessageV2 = 310,
    GetUnitTopicList = 311,
    GetHasUnitSubTopicList = 312,
    GetHasUnitSubUnunitTopicList = 313,
    CloneGroupOffset = 314,
    ViewBrokerStatsData = 315,
    CleanUnusedTopic = 316,
    GetBrokerConsumeStats = 317,
    UpdateNamesrvConfig = 318,
    GetNamesrvConfig = 319,
    SendBatchMessage = 320,
    QueryConsumeQueue = 321,
    QueryDataVersion = 322,
    ResumeCheckHalfMessage = 323,
    SendReplyMessage = 324,
    SendReplyMessageV2 = 325,
    PushReplyMessageToClient = 326,
    AddWritePermOfBroker = 327,
    GetTopicConfig = 351,
    GetSubscriptionGroupConfig = 352,
    UpdateAndGetGroupForbidden = 353,
    LitePullMessage = 361,
    QueryAssignment = 400,
    SetMessageRequestMode = 401,
    GetAllMessageRequestMode = 402,
    UpdateAndCreateStaticTopic = 513,
    GetBrokerMemberGroup = 901,
    AddBroker = 902,
    RemoveBroker = 903,
    BrokerHeartbeat = 904,
    NotifyMinBrokerIdChange = 905,
    ExchangeBrokerHaInfo = 906,
    GetBrokerHaStatus = 907,
    ResetMasterFlushOffset = 908,
    GetAllProducerInfo = 328,
    DeleteExpiredCommitlog = 329,

    UpdateColdDataFlowCtrConfig = 2001,
    RemoveColdDataFlowCtrConfig = 2002,
    GetColdDataFlowCtrInfo = 2003,
    SetCommitlogReadMode = 2004,
    Unknown = -9999999,
}

impl From<RequestCode> for i32 {
    fn from(value: RequestCode) -> Self {
        value as i32
    }
}

impl From<i32> for RequestCode {
    fn from(value: i32) -> Self {
        match value {
            10 => RequestCode::SendMessage,
            11 => RequestCode::PullMessage,
            12 => RequestCode::QueryMessage,
            13 => RequestCode::QueryBrokerOffset,
            14 => RequestCode::QueryConsumerOffset,
            15 => RequestCode::UpdateConsumerOffset,
            17 => RequestCode::UpdateAndCreateTopic,
            21 => RequestCode::GetAllTopicConfig,
            22 => RequestCode::GetTopicConfigList,
            23 => RequestCode::GetTopicNameList,
            25 => RequestCode::UpdateBrokerConfig,
            26 => RequestCode::GetBrokerConfig,
            27 => RequestCode::TriggerDeleteFiles,
            28 => RequestCode::GetBrokerRuntimeInfo,
            29 => RequestCode::SearchOffsetByTimestamp,
            30 => RequestCode::GetMaxOffset,
            31 => RequestCode::GetMinOffset,
            32 => RequestCode::GetEarliestMsgStoreTime,
            33 => RequestCode::ViewMessageById,
            34 => RequestCode::HeartBeat,
            35 => RequestCode::UnregisterClient,
            36 => RequestCode::ConsumerSendMsgBack,
            37 => RequestCode::EndTransaction,
            38 => RequestCode::GetConsumerListByGroup,
            39 => RequestCode::CheckTransactionState,
            40 => RequestCode::NotifyConsumerIdsChanged,
            41 => RequestCode::LockBatchMq,
            42 => RequestCode::UnlockBatchMq,
            43 => RequestCode::GetAllConsumerOffset,
            45 => RequestCode::GetAllDelayOffset,
            46 => RequestCode::CheckClientConfig,
            47 => RequestCode::GetClientConfig,
            50 => RequestCode::UpdateAndCreateAclConfig,
            51 => RequestCode::DeleteAclConfig,
            52 => RequestCode::GetBrokerClusterAclInfo,
            53 => RequestCode::UpdateGlobalWhiteAddrsConfig,
            54 => RequestCode::GetBrokerClusterAclConfig,
            60 => RequestCode::GetTimerCheckPoint,
            61 => RequestCode::GetTimerMetrics,
            200050 => RequestCode::PopMessage,
            200051 => RequestCode::AckMessage,
            200151 => RequestCode::BatchAckMessage,
            200052 => RequestCode::PeekMessage,
            200053 => RequestCode::ChangeMessageInvisibleTime,
            200054 => RequestCode::Notification,
            200055 => RequestCode::PollingInfo,
            100 => RequestCode::PutKvConfig,
            101 => RequestCode::GetKvConfig,
            102 => RequestCode::DeleteKvConfig,
            103 => RequestCode::RegisterBroker,
            104 => RequestCode::UnregisterBroker,
            105 => RequestCode::GetRouteinfoByTopic,
            106 => RequestCode::GetBrokerClusterInfo,
            200 => RequestCode::UpdateAndCreateSubscriptionGroup,
            201 => RequestCode::GetAllSubscriptionGroupConfig,
            202 => RequestCode::GetTopicStatsInfo,
            203 => RequestCode::GetConsumerConnectionList,
            204 => RequestCode::GetProducerConnectionList,
            205 => RequestCode::WipeWritePermOfBroker,
            206 => RequestCode::GetAllTopicListFromNameserver,
            207 => RequestCode::DeleteSubscriptionGroup,
            208 => RequestCode::GetConsumeStats,
            209 => RequestCode::SuspendConsumer,
            210 => RequestCode::ResumeConsumer,
            211 => RequestCode::ResetConsumerOffsetInConsumer,
            212 => RequestCode::ResetConsumerOffsetInBroker,
            213 => RequestCode::AdjustConsumerThreadPool,
            214 => RequestCode::WhoConsumeTheMessage,
            215 => RequestCode::DeleteTopicInBroker,
            216 => RequestCode::DeleteTopicInNamesrv,
            217 => RequestCode::RegisterTopicInNamesrv,
            219 => RequestCode::GetKvlistByNamespace,
            220 => RequestCode::ResetConsumerClientOffset,
            221 => RequestCode::GetConsumerStatusFromClient,
            222 => RequestCode::InvokeBrokerToResetOffset,
            223 => RequestCode::InvokeBrokerToGetConsumerStatus,
            300 => RequestCode::QueryTopicConsumeByWho,
            224 => RequestCode::GetTopicsByCluster,
            343 => RequestCode::QueryTopicsByConsumer,
            345 => RequestCode::QuerySubscriptionByConsumer,
            301 => RequestCode::RegisterFilterServer,
            302 => RequestCode::RegisterMessageFilterClass,
            303 => RequestCode::QueryConsumeTimeSpan,
            304 => RequestCode::GetSystemTopicListFromNs,
            305 => RequestCode::GetSystemTopicListFromBroker,
            306 => RequestCode::CleanExpiredConsumequeue,
            307 => RequestCode::GetConsumerRunningInfo,
            308 => RequestCode::QueryCorrectionOffset,
            309 => RequestCode::ConsumeMessageDirectly,
            310 => RequestCode::SendMessageV2,
            311 => RequestCode::GetUnitTopicList,
            312 => RequestCode::GetHasUnitSubTopicList,
            313 => RequestCode::GetHasUnitSubUnunitTopicList,
            314 => RequestCode::CloneGroupOffset,
            315 => RequestCode::ViewBrokerStatsData,
            316 => RequestCode::CleanUnusedTopic,
            317 => RequestCode::GetBrokerConsumeStats,
            318 => RequestCode::UpdateNamesrvConfig,
            319 => RequestCode::GetNamesrvConfig,
            320 => RequestCode::SendBatchMessage,
            321 => RequestCode::QueryConsumeQueue,
            322 => RequestCode::QueryDataVersion,
            323 => RequestCode::ResumeCheckHalfMessage,
            324 => RequestCode::SendReplyMessage,
            325 => RequestCode::SendReplyMessageV2,
            326 => RequestCode::PushReplyMessageToClient,
            327 => RequestCode::AddWritePermOfBroker,
            351 => RequestCode::GetTopicConfig,
            352 => RequestCode::GetSubscriptionGroupConfig,
            353 => RequestCode::UpdateAndGetGroupForbidden,
            361 => RequestCode::LitePullMessage,
            400 => RequestCode::QueryAssignment,
            401 => RequestCode::SetMessageRequestMode,
            402 => RequestCode::GetAllMessageRequestMode,
            513 => RequestCode::UpdateAndCreateStaticTopic,
            901 => RequestCode::GetBrokerMemberGroup,
            902 => RequestCode::AddBroker,
            903 => RequestCode::RemoveBroker,
            904 => RequestCode::BrokerHeartbeat,
            905 => RequestCode::NotifyMinBrokerIdChange,
            906 => RequestCode::ExchangeBrokerHaInfo,
            907 => RequestCode::GetBrokerHaStatus,
            908 => RequestCode::ResetMasterFlushOffset,
            328 => RequestCode::GetAllProducerInfo,
            329 => RequestCode::DeleteExpiredCommitlog,
            2001 => RequestCode::UpdateColdDataFlowCtrConfig,
            2002 => RequestCode::RemoveColdDataFlowCtrConfig,
            2003 => RequestCode::GetColdDataFlowCtrInfo,
            2004 => RequestCode::SetCommitlogReadMode,
            _ => RequestCode::Unknown,
        }
    }
}

impl RequestCode {
    pub fn to_i32(self) -> i32 {
        self.into()
    }

    pub fn value_of(code: i32) -> Option<Self> {
        Some(code.into())
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ControllerRequestCode {
    ControllerAlterSyncStateSet = 1001,
    ControllerElectMaster = 1002,
    ControllerRegisterBroker = 1003,
    ControllerGetReplicaInfo = 1004,
    ControllerGetMetadataInfo = 1005,
    ControllerGetSyncStateData = 1006,
    GetBrokerEpochCache = 1007,
    NotifyBrokerRoleChanged = 1008,
    UpdateControllerConfig = 1009,
    GetControllerConfig = 1010,
    CleanBrokerData = 1011,
    ControllerGetNextBrokerId = 1012,
    ControllerApplyBrokerId = 1013,
}
